package com.alertdroid2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class addMemoActivity extends Activity {
	DatePicker datePicker;
	TimePicker timePicker;
	Spinner spinner;
	EditText txtName;
	int[] idSpinner;
	DatabaseHelper myDbHelper;
	Calendar calendar = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memo);

		try {
			// initdb
			initDb();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ArrayAdapter<String> adapterPlace = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item,
				myDbHelper.readNamePlace());
		idSpinner = myDbHelper.readIdPlace();

		// spinner place
		spinner = (Spinner) findViewById(R.id.SpinnerLoc);
		spinner.setAdapter(adapterPlace);

		datePicker = (DatePicker) findViewById(R.id.DatePicker01);

		timePicker = (TimePicker) findViewById(R.id.TimePicker01);
		timePicker.setIs24HourView(true);

		txtName = (EditText) findViewById(R.id.txtNamaMemo);

		Button btn = (Button) findViewById(R.id.btnSaveMemo);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int idplace = idSpinner[spinner.getSelectedItemPosition()];
				Time time = new Time();
				time.set(0, timePicker.getCurrentMinute(),
						timePicker.getCurrentHour(),
						datePicker.getDayOfMonth(), datePicker.getMonth(),
						datePicker.getYear());
				String name = txtName.getText().toString();

				// save to db
				myDbHelper.insertMemo(name, time, idplace);

				// set calendar
				calendar.setTimeInMillis(System.currentTimeMillis());
				calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
				calendar.set(Calendar.MONTH, datePicker.getMonth());
				calendar.set(Calendar.YEAR, datePicker.getYear());
				calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
				calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				
				//get idMemo from db
				MemoClass memoClass = myDbHelper.getMemo(name, time, idplace);
				int idMemo = memoClass.idMemo;				

				Intent intent = new Intent(addMemoActivity.this,
						CallAlarm.class);
				//save idMemo ke intent
				intent.putExtra(alertdroidConstants.IDMEMO, idMemo);
				PendingIntent sender = PendingIntent.getBroadcast(
						addMemoActivity.this, 0, intent, 0);
				AlarmManager am;
				am = (AlarmManager) getSystemService(ALARM_SERVICE);
				// set alarm
				am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
						sender);

				Toast.makeText(getBaseContext(), "Memo : " + name + " saved.",
						Toast.LENGTH_SHORT).show();

				finish();
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		myDbHelper.closeDb();
		super.onDestroy();
	}

	private void initDb() throws IOException {
		myDbHelper = new DatabaseHelper(this);
		myDbHelper.createDataBase();
		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
	}
}
